<template>
	<div class="az">
		<div class="aa" style="width: 500px; margin-left: 400px;">
			<div id="app">
				<div class="i1" style="margin-bottom: 15px;">
					<router-link to="index" style="color: white;">登录</router-link>&nbsp;&nbsp;|&nbsp;&nbsp;<router-link to="login"
					 style="color: white;">注册</router-link>
				</div>
			</div>
			<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
				<p v-if="ruleForm.err" style="height: 20px;color: red;">{{ruleForm.err}}</p>
				<el-form-item label="账号" prop="username">
					<el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
				</el-form-item>

				<el-form-item label="密码" prop="pass">
					<el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
				</el-form-item>
				<p>
					<router-link to="login">没有账号？注册</router-link>
				</p>
				<el-form-item>
					<el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
					<el-button @click="resetForm('ruleForm')">重置</el-button>
				</el-form-item>
			</el-form>
		</div>
	</div>
</template>
<script>
	import axios from 'axios'

	export default {
		data() {
			var checkAge = (rule, value, callback) => {
				if (!value) {
					return callback(new Error('年龄不能为空'));
				}
				setTimeout(() => {
					if (!Number.isInteger(value)) {
						callback(new Error('请输入数字值'));
					} else {
						if (value < 18) {
							callback(new Error('必须年满18岁'));
						} else {
							callback();
						}
					}
				}, 1000);
			};
			var validatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入密码'));
				} else {
					if (this.ruleForm.checkPass !== '') {
						this.$refs.ruleForm.validateField('checkPass');
					}
					callback();
				}
			};
			var validatePass2 = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请再次输入密码'));
				} else if (value !== this.ruleForm.pass) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};
			return {
				ruleForm: {
					err: '',
					pass: '',
					checkPass: '',
					age: '',
					username: ''
				},
				rules: {
					pass: [{
						validator: validatePass,
						trigger: 'blur'
					}],
					checkPass: [{
						validator: validatePass2,
						trigger: 'blur'
					}],
					age: [{
						validator: checkAge,
						trigger: 'blur'
					}]
				}
			};
		},
		methods: {
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						const us = this.ruleForm.username;
						const ps = this.ruleForm.pass;
						var err00 = ""
						var success = 0
                        
						let this_01 = this

						try {
							const user = axios.get("/login?username=" + us + "&password=" + ps)
								.then(function(data) {
							
									console.log(data);
									 var  state=data.data[0].state;
									const user = data.data;
									if (user === '' || user === null || user.length === 0) {
										err00 = "账号或者密码错误。。。"
									}
									
									if (state == 0) {
										if (user.length > 0) {
											localStorage.setItem("user", user)
											console.log(user);
											localStorage.setItem("userId", user[0].id)
											localStorage.setItem("username", user[0].username)
											success = 1
										}
										if (success === 1) {
											this_01.$router.push({
												path: '/'
											})
											alert('submit!');
										} else if (success === 0) {
											console.log(err00);
											this_01.ruleForm.err = err00
											console.log(this_01.ruleForm.err);
										}
									} else if (state == 1) {
										this_01.ruleForm.err ="你的账号已被登录";
										console.log(this_01.ruleForm.err)
									} else if (state == 2) {
										this_01.ruleForm.err = "你的账号已被注销"
									}


								}).catch(function(Error) {
									console.log(Error);
								});

						} catch (e) {
							console.log(e);
							//TODO handle the exception
						}
						console.log(success);


						this.ruleForm.err = err00;


					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		}

	}
</script>
<style>
	.az {
		width: 100%;
		height: 725px;
		background: url(../../images/009.jpg);
		padding-top: 50px;
	}
</style>
